package com.hm.pan.task;

import com.hm.pan.util.HttpURLUtil;

/**
 * 离线下载线程
 */
public class OfflineThread implements Runnable {

	/**
	 * 成功回调
	 */
	public static interface SuccessCallback {
		void callback(byte[] data, String url);
	}

	/**
	 * 失败回调
	 */
	public static interface FailCallback {
		void callback();
	}

	private String url;
	private SuccessCallback successCallback;
	private FailCallback failCallback;

	public OfflineThread(String url, SuccessCallback successCallback, FailCallback failCallback) {
		if (url == null || successCallback == null || failCallback == null) {
			throw new NullPointerException("url, successCallback or failCallback cannot be null!");
		}

		this.url = url;
		this.successCallback = successCallback;
		this.failCallback = failCallback;
	}

	@Override
	public void run() {
		byte[] get = HttpURLUtil.doGet(url);

		if (get == null) {
			failCallback.callback();
			return;
		}

		successCallback.callback(get, url);
	}
}
